#include "filltool.h"
//#include <map>

#define IS_SUCCESSFUL true
#define MSG_COLOURED "Voxels have been coloured"
#define MSG_NOT_COLOURED "Voxels have not been coloured"

FillTool::FillTool()
{
    rend=RenderManager::getInstance();
    global=GlobalStates::getInstance();
}

FillTool *FillTool::getInstance() {
    static FillTool* inst=0;
    if(!inst)inst = new FillTool();
    return inst;
}

string FillTool::getType() {
    return "fill";
}

Result FillTool::mousePressEvent(QMouseEvent *event) {
    colour col = global->getRGB();
    vector3<int> coordsFloat = rend->mouseUtils.getMouseOverVoxelPt(event->x(), event->y());
    int x = (int)coordsFloat.x;
    int y = (int)coordsFloat.y;
    int z = (int)coordsFloat.z;
    vector3<int> coords = vector3<int>(x,y,z);
    const set< vector3<int> >& currentSelection = global->getSelection();

    if(!currentSelection.empty()) {
        if(currentSelection.find(coords)!=currentSelection.end()) { //if cursor is pointing at a voxel in the selection
            global->changeAllSelectedVoxelsColour(col);
            return Result(IS_SUCCESSFUL, MSG_COLOURED);
        } else { //cursor pointing at empty space in selection
            return Result(IS_SUCCESSFUL, MSG_NOT_COLOURED);
        }
    } else {
        voxel& v = rend->getVoxel(coords.x, coords.y, coords.z);
        if(v.draw()) {
            //colour voxel
            rend->setVoxelColor(coords.x, coords.y, coords.z, col);
            return Result(IS_SUCCESSFUL, MSG_COLOURED);
        } else {
            //nothing coloured
            return Result(IS_SUCCESSFUL, MSG_NOT_COLOURED);
        }
    }
}
